package ph.com.gs3.chat;

import ph.com.gs3.chat.fragments.view.LoginViewFragment;
import ph.com.gs3.chat.fragments.view.LoginViewFragment.LoginViewActionListener;
import ph.com.gs3.chat.fragments.worker.LoginWorkerFragment;
import ph.com.gs3.chat.fragments.worker.LoginWorkerFragment.LoginWorkerListener;
import ph.com.gs3.chat.model.valueobject.business.User;
import ph.com.gs3.chat.utilities.FLLogger;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public class LoginActivity extends Activity implements LoginViewActionListener, LoginWorkerListener {

	public static final String TAG = LoginActivity.class.getSimpleName();

	private LoginViewFragment viewfragment;
	private LoginWorkerFragment workerFragment;

	// =========================================================================
	// {{ Life Cycle Listeners

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initializeFragments(savedInstanceState);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	// }}

	// =========================================================================
	// {{ Initializers

	private void initializeFragments(Bundle savedInstanceState) {

		FragmentManager manager = getFragmentManager();
		FragmentTransaction transaction = manager.beginTransaction();

		// set the view fragment
		if (savedInstanceState == null) {
			viewfragment = LoginViewFragment.createInstance();
			transaction.add(R.id.container, viewfragment);
		}

		// set the worker fragment
		workerFragment = (LoginWorkerFragment) manager.findFragmentByTag(LoginWorkerFragment.TAG);
		if (workerFragment == null) {
			workerFragment = LoginWorkerFragment.createInstance();
		}

		transaction.add(workerFragment, LoginWorkerFragment.TAG);

		transaction.commit();

	}

	// }}

	// =========================================================================
	// {{ View Listeners

	@Override
	public void onLoginCommand(String server, String email, String password) {
		workerFragment.login(server, email, password);
		viewfragment.displayLoginProgress();
	}

	// }}

	// =========================================================================
	// {{ Worker Fragment Listeners

	@Override
	public void onLoginFailed(String message) {
		viewfragment.displayErrorMessage(message);
		viewfragment.displayLoginFields();
	}

	@Override
	public void onLoginSuccess(User loggedInUser) {
		FLLogger.d(TAG, loggedInUser.getDisplayName() + " logged in");
		// viewfragment.displayLoginFields();

		Intent mainActivityIntent = new Intent(LoginActivity.this, MainActivity.class);
		// Intent mainActivityIntent = new Intent(LoginActivity.this, TestActivity.class);
		mainActivityIntent.putExtra(MainActivity.EXTRA_ACTIVE_USER, loggedInUser);
		mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(mainActivityIntent);

		finish();

	}

	// }}

}
